package taglib;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

@SuppressWarnings("serial")
public class OperatorsTag extends TagSupport {

	private String formName = "frm";
	private String postUrl;
	private String postMessage;
	private String params;
	
	public int doStartTag()  {
		return (SKIP_BODY);
	}
	
	public int doEndTag()  {
		
		JspWriter writer = pageContext.getOut();
		StringBuffer sbTR = new StringBuffer();
		sbTR.append("<a href=\"#\" onclick=\"CheckAll('ids')\">全选</a>&nbsp;|&nbsp;<a href=\"#\" onclick=\"UnCheckAll('ids')\">不选</a>");
		
		if(StringUtils.isEmpty(postMessage)){
			sbTR.append("&nbsp;|&nbsp;");
			String buttonMsg = "删除";
			sbTR.append("<a href=\"#\" onclick=\"if(confirm('确认"+buttonMsg+"吗？'))document.forms."+formName+".action='"+postUrl+"';document.forms."+formName+".submit();return false;\">"+buttonMsg+"</a>");
		}
		else{
			String[] urls = StringUtils.split(postUrl,",");
			String[] msgs  = StringUtils.split(postMessage,",");
			
			for(int i = 0;i<urls.length;i++){
				String url = urls[i];
				String msg = msgs[i];
				sbTR.append("<a href=\"#\" onclick=\"if(confirm('确认"+msg+"吗？'))document.forms."+formName+".action='"+url+"';document.forms."+formName+".submit();return false;\">"+msg+"</a>");
			}
		}
		try {
			writer.write(sbTR.toString());
		} catch (Exception e) {

		}
		return (SKIP_BODY);
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getPostMessage() {
		return postMessage;
	}

	public void setPostMessage(String postMessage) {
		this.postMessage = postMessage;
	}

	public String getPostUrl() {
		return postUrl;
	}

	public void setPostUrl(String postUrl) {
		this.postUrl = postUrl;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}
}
